package time.code.classic;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;


/**
 * Description: Calendar示例
 *
 * @author code-worker
 * @date 2021/08/21 18:10
 */
public class CalendarDemo {

    private static final Map<Integer, String> WEEK_MAP = new HashMap<>(7);
    static {
        WEEK_MAP.put(1, "周日");
        WEEK_MAP.put(2, "周一");
        WEEK_MAP.put(3, "周二");
        WEEK_MAP.put(4, "周三");
        WEEK_MAP.put(5, "周四");
        WEEK_MAP.put(6, "周五");
        WEEK_MAP.put(7, "周六");
    }

    /**
     * task: 获取当前上海时间
     * @date 2021/8/21 18:20
     */
    public void getNowCalendar(){
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        System.out.println("当前上海时间: "+calendar.getTime());
    }

    /**
     * task: 获取当前上海时间(格式化)
     * @date 2021/8/21 18:25
     */
    public void getNowCalendarWithFormat(){
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        System.out.println("当前上海时间："
                +calendar.get(Calendar.YEAR) + "-"
                + calendar.get(Calendar.MONTH) + "-"
                + calendar.get(Calendar.DAY_OF_MONTH) + " "
                + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                + calendar.get(Calendar.MINUTE) + ":"
                + calendar.get(Calendar.SECOND));
    }

    /**
     * task: 自定义时间
     * @date 2021/8/21 18:28
     * @param year 年
     * @param month 月
     * @param day 日
     * @param hour 小时
     * @param minute 分钟
     * @param second 秒
     */
    public void setCalendar(int year, int month, int day, int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
        System.out.println("当前设置的时间: "+calendar.getTime());
    }

    /**
     * task: 自定义加一秒后的时间
     * @date 2021/8/21 18:30
     * @param year 年
     * @param month 月
     * @param day 日
     * @param hour 小时
     * @param minute 分钟
     * @param second 秒
     */
    public void addOneSecond(int year, int month, int day, int hour, int minute, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
        System.out.println("当前设置的时间: "+calendar.getTime());
        calendar.add(Calendar.SECOND, 1);
        System.out.println("当前加一秒后的时间: "+calendar.getTime());
    }

    public void getWeek(){
        Calendar calendar = Calendar.getInstance();
        System.out.println("今天周几？" + WEEK_MAP.get(calendar.get(Calendar.DAY_OF_WEEK)));
    }

    public static void main(String[] args) {
        CalendarDemo demo = new CalendarDemo();
        demo.getNowCalendar();
        demo.getNowCalendarWithFormat();
        demo.setCalendar(2021, 7, 21, 18, 27, 36);
        demo.addOneSecond(2021, 7, 21, 18, 27, 59);
        demo.getWeek();
    }

}
